package com.zhijian.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhijian.entity.CustomerService;
import com.zhijian.entity.R;
import com.zhijian.service.ICustomerServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/customerService")
public class AdminCustomerServiceController {

    @Autowired
    private ICustomerServiceService customerServiceService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @PostMapping("/login")
    public R login(@RequestBody CustomerService customerService) {
        CustomerService resultCustomerService = customerServiceService.login(customerService.getUserName(), customerService.getPassword());
        if(resultCustomerService == null) {
            return R.error("用户名或密码错误");
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("userInfo", resultCustomerService);
        return R.ok(resultMap);
    }

    @GetMapping("/list")
    public R list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                  @RequestParam(value = "pageSize", defaultValue = "101") Integer pageSize,
                  @RequestParam(value = "query", required = false) String query) {
        Page<CustomerService> page = new Page<>(pageNum, pageSize);
        QueryWrapper<CustomerService> queryWrapper = new QueryWrapper<>();
        if(query != null) {
            queryWrapper.like("username", query).or().like("realName", query);
        }
        queryWrapper.orderByDesc("createTime");

        // 将分页结果转换为Map
        Page<CustomerService> pageResult = customerServiceService.page(page, queryWrapper);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("records", pageResult.getRecords());
        resultMap.put("total", pageResult.getTotal());
        resultMap.put("size", pageResult.getSize());
        resultMap.put("current", pageResult.getCurrent());
        resultMap.put("pages", pageResult.getPages());

        return R.ok(resultMap);
    }

    @PostMapping("/add")
    public R add(@RequestBody CustomerService customerService) {
        String now = sdf.format(new Date());
        customerService.setCreateTime(now);
        customerService.setUpdateTime(now);
        customerServiceService.save(customerService);
        return R.ok();
    }

    @PostMapping("/update")
    public R update(@RequestBody CustomerService customerService) {
        customerService.setUpdateTime(sdf.format(new Date()));
        customerServiceService.updateById(customerService);
        return R.ok();
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id") Integer id) {
        customerServiceService.removeById(id);
        return R.ok();
    }
}
